package ldh.serialize.type;

import java.io.IOException;

import ldh.serialize.BinarySerialized;
import ldh.serialize.io.ByteBuf;
import ldh.serialize.util.CompressionUtil;
import ldh.serialize.util.SerializedUtil;

public class VarIntegerSerialized  implements BinarySerialized<Integer> {

	@Override
	public void write(ByteBuf out, Integer num) throws IOException {
		if (num == null) {
			SerializedUtil.writeByte(out, (byte)0);
			return;
		}
		byte[] bbs = CompressionUtil.intToByte(num);
		SerializedUtil.writeBytes(out, bbs);
	}

	@Override
	public Integer read(ByteBuf in) throws IOException {
		byte isNull = SerializedUtil.readByte(in);
		if (isNull < 1) {
			return null;
		}
		return CompressionUtil.byteToVarInt(in);
	}

	@Override
	public Class<Integer> getClassType() {
		return Integer.class;
	}
}
